#include <string.h> 
#include <stdio.h> 
#include <stdlib.h> 
#include <unistd.h> 

#include "config.h"
#include "xml_client.h"
#include "sys_log.h"

void* create_xml_handler()
{
  xmlDocPtr doc = NULL;
  xmlNodePtr root_node = NULL;
  doc = xmlNewDoc(BAD_CAST "1.0");
  root_node = xmlNewNode(NULL, BAD_CAST "root");
  xmlDocSetRootElement(doc, root_node);
  return doc;
}

void* create_xml_node_from_handler(void* xmlhandler,char* node_name, char* node_value)
{
	xmlDocPtr doc = (xmlDocPtr)xmlhandler;
	xmlNodePtr root_node=xmlDocGetRootElement(doc);
	xmlNodePtr node = NULL;

	if (node_name != NULL) {
	    node = xmlNewNode(NULL,BAD_CAST node_name);
		if (node_value != NULL) {
    		xmlNodePtr content = xmlNewText(BAD_CAST node_value);
			xmlAddChild(node,content);
		}
	    xmlAddChild(root_node,node);
	}
	return node;
}


void* create_xml_node_from_node(void* nodehandler,char* node_name, char* node_value)
{
	xmlNodePtr parent_node=(xmlNodePtr)nodehandler;
	xmlNodePtr node = NULL;

	if (node_name != NULL) {
	    node = xmlNewNode(NULL,BAD_CAST node_name);
		if (node_value != NULL) {
			xmlNodePtr content = xmlNewText(BAD_CAST node_value);
	    	xmlAddChild(node,content);
		}
		xmlAddChild(parent_node,node);
	}
	return node;
}


char* create_xml_file_from_handler(void* xmlhandler, char* filename)
{
	xmlDocPtr doc = (xmlDocPtr)xmlhandler;
    int nRel = xmlSaveFile(filename,doc);

    if (nRel != -1) {
        SYS_LOG("create_xml_file_from_handler file_size[%d]\n",nRel);
	}
    xmlFreeDoc(doc);
}


void* parse_xml_buf(char *buf,int len)
{
	xmlDocPtr doc = xmlParseMemory(buf,len);    //parse xml in memory
	if(doc==NULL){
        SYS_LOG("doc == null\n");
        return NULL;
	}
	return (void*)doc;
}

void* parse_get_xml_node_value_from_handler(void* xmlhandler, char* node_name, char** node_value)
{
	xmlNodePtr node;
	xmlDocPtr doc = (xmlDocPtr)xmlhandler;
	if(NULL == doc) return NULL;

	xmlNodePtr root = xmlDocGetRootElement(doc);
	if(NULL == root) return NULL;
	
	for(node=root->children;node;node=node->next){
	   if(xmlStrcasecmp(node->name,BAD_CAST node_name)==0) {
	   		if (node_value != NULL)
			*node_value = xmlNodeGetContent(node);
	        return (void*)node;
	   	}
	}
	return NULL;
}

void* parse_get_xml_node_value_from_node(void* xml_node, char* node_name, char** node_value)
{
	xmlNodePtr node;
	if (xml_node == NULL) return NULL;
	for(node=((xmlNodePtr)xml_node)->children;node;node=node->next){
	   if(xmlStrcasecmp(node->name,BAD_CAST node_name)==0) {
		   if (node_value != NULL)
			*node_value = xmlNodeGetContent(node);
	        return (void*)node;
	   	}
	}
	return NULL;
}

void* parse_get_xml_node_value_next(void* xml_node, char* node_name, char** node_value)
{
	xmlNodePtr node;
	if (xml_node == NULL) return NULL;
	for(node=((xmlNodePtr)xml_node)->next;node;node=node->next){
	   if(xmlStrcasecmp(node->name,BAD_CAST node_name)==0) {
		   if (node_value != NULL)
			*node_value = xmlNodeGetContent(node);
	        return (void*)node;
	   	}
	}
	return NULL;
}



int parse_xml_buf_finish(void* xmlhandler)
{
	xmlDocPtr doc = (xmlDocPtr)xmlhandler;    //parse xml in memory
	xmlFreeDoc(doc);
	return 1;
}

#if 0
void* test_xml(void)
{
    char *content;
	char *tmp_str;
    unsigned long filesize;
    FILE *file;
	void* xml_handler;

	xml_handler = create_xml_handler();
	create_xml_node_from_handler(xml_handler,"command","print");
	create_xml_node_from_handler(xml_handler,"command","restart");
	void* download_node = create_xml_node_from_handler(xml_handler,"download", NULL);

	create_xml_node_from_node(download_node,"pic1","www.baidu.com/1.jpg");
	create_xml_node_from_node(download_node,"pic2","www.baidu.com/2.jpg");
	create_xml_node_from_node(download_node,"pic3","www.baidu.com/3.jpg");
	create_xml_file_from_handler(xml_handler,"./test.xml");

	
    if((file=fopen("./test.xml","r"))==NULL){
        SYS_LOG("openf file error");
    }
    fseek(file,0,SEEK_END);
    filesize=ftell(file);
    rewind(file);
    content=(char *)malloc(filesize+1);
    memset(content,0,filesize+1);
    fread(content,1,filesize,file);
    fclose(file);
    SYS_LOG("content:%s\n",content);
    if((xml_handler = parse_xml_buf(content,filesize)) == NULL){
            SYS_LOG("parse xml failed");
    }
	parse_get_xml_node_value_from_handler(xml_handler,"command",&tmp_str);

	parse_get_xml_node_value_from_handler(xml_handler,"pic1",&tmp_str);
	
	download_node = parse_get_xml_node_value_from_handler(xml_handler,"download",&tmp_str);

	parse_get_xml_node_value_from_node(download_node,"pic2",&tmp_str);

	parse_xml_buf_finish(xml_handler);
	
	free(content);
    return 0;
}
#else
void* test_xml(void)
{
}

#endif

